﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DuLieu;
using System.Data.OleDb;

namespace DAO
{
    public static class TenDiaDiemDAO
    {
        public static TenDiaDiemDTO GetTenDiaDiemById(int id)
        {
            TenDiaDiemDTO dd = null;

            try
            {
                String sql = "select * from TenDiaDiem where MaTenDiaDiem = " + id;
                DataProvider.OpenConnection();
                OleDbDataReader reader = DataProvider.ExecuteReader(sql);

                if (reader.Read())
                {
                    dd = new TenDiaDiemDTO();
                    if (!reader.IsDBNull(0))
                        dd.MaTenDiaDiem = reader.GetInt32(0);
                    if (!reader.IsDBNull(1))
                        dd.TenDiaDiem = reader.GetString(1);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                DataProvider.CloseConnection();
            }

            return dd;
        }

        public static String GetNameById(int id)
        {
            String result = null;

            try
            {
                String sql = "select TenDiaDiem from TenDiaDiem where MaTenDiaDiem = " + id;
                DataProvider.OpenConnection();
                OleDbDataReader reader = DataProvider.ExecuteReader(sql);

                if (reader.Read())
                {
                    if (!reader.IsDBNull(0))
                        result = reader.GetString(0);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                DataProvider.CloseConnection();
            }

            return result;
        }

        public static int GetIdByTenDiaDiem(string ten)
        {
            TenDiaDiemDTO dd = null;

            try
            {
                String sql = "select * from TENDIADIEM where TenDiaDiem = " + "'" + ten + "'";
                DataProvider.OpenConnection();
                OleDbDataReader reader = DataProvider.ExecuteReader(sql);

                if (reader.Read())
                {
                    dd = new TenDiaDiemDTO();
                    if (!reader.IsDBNull(0))
                        dd.MaTenDiaDiem = reader.GetInt32(0);
                    if (!reader.IsDBNull(1))
                        dd.TenDiaDiem = reader.GetString(1);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                DataProvider.CloseConnection();
            }

            return dd.MaTenDiaDiem;
        }

        public static List<TenDiaDiemDTO> GetDanhSachTenDiaDiem()
        {
            List<TenDiaDiemDTO> list = new List<TenDiaDiemDTO>();

            try
            {
                string sql = "select * from TenDiaDiem";
                DataProvider.OpenConnection();
                OleDbDataReader reader = DataProvider.ExecuteReader(sql);

                while (reader.Read())
                {
                    TenDiaDiemDTO dd = new TenDiaDiemDTO();
                    if (!reader.IsDBNull(0))
                    {
                        dd.MaTenDiaDiem = reader.GetInt32(0);
                    }
                    if (!reader.IsDBNull(1))
                    {
                        dd.TenDiaDiem = reader.GetString(1);
                    }
                    list.Add(dd);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                DataProvider.CloseConnection();
            }

            return list;
        }

        public static Boolean InsertTenDiaDiem(TenDiaDiemDTO dd)
        {
            TenDiaDiemDTO temp = GetTenDiaDiemById(dd.MaTenDiaDiem);
            if (temp != null)
                return false;

            try
            {
                string query = "insert into TenDiaDiem(MaTenDiaDiem,TenDiaDiem) values(" + dd.MaTenDiaDiem + "," + "'" + dd.TenDiaDiem + "'" + ")";
                DataProvider.OpenConnection();
                DataProvider.ExecuteNonQuery(query);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                DataProvider.CloseConnection();
            }
            return true;
        }

        public static Boolean DeleteTenDiaDiemById(int id)
        {
            TenDiaDiemDTO temp = GetTenDiaDiemById(id);
            if (temp == null)
                return false;

            try
            {
                String sql = "delete from TenDiaDiem where MaTenDiaDiem=" + id;
                DataProvider.OpenConnection();
                DataProvider.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                DataProvider.CloseConnection();
            }

            return true;
        }
    }
}
